package com.test.dababy.dababy_05_process.widget;

import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;
import static com.test.dababy.dababy_00_utils.DababyUtil.stopLoading;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.test.dababy.R;
import com.test.dababy.dababy_00_utils.DababyUtil;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_01_launcher.Base_dababyApplication;
import com.test.dababy.dababy_05_process.Proces_dababysIn_dababyfo_Data;
import com.test.dababy.dababy_05_process.Acc_dababyountLi_dababyst_Fr;
import com.test.dababy.dababy_05_process.Ba_dababynkAcc_dababyount_Fr;
import com.test.dababy.Othe_dababyrsInfo;
import com.test.dababy.dababy_05_process.Bin_dababydAcco_dababyuntChannelDialog_Fr;
import com.test.dababy.dababy_05_process.Bin_dababydAccount_dababyDialog_Fr;
import com.test.dababy.dababy_05_process.Delete_dababyAccountDi_dababyalog_Fr;
import com.test.dababy.dababy_00_http.OkHttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class BindAcco_dababyuntViewM_dababyanager_Fr extends BaseProc_dababyessWid_dababygetManager {

    private String hi893 ="";
    private char hjlr90t ='4';
    private boolean hkjeo =false;
    private String hyz45kg ="";
    private String h89rj ="";
    public void haryhjkhr(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getKtysgb()))
                    setHeser(String.valueOf(getHuiyhegh3())+ getHsrrt6()+ isHkjeo());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }
    public void setKtysgb(String ktysgb) {this.hi893 = ktysgb;}
    public void setHuiyhegh3(char huiyhegh3) {this.hjlr90t = huiyhegh3;}
    public void setHkjeo(boolean hkjeo) {this.hkjeo = hkjeo;}
    public void setHeser(String heser) {this.hyz45kg = heser;}
    public void setHaerttr(String haerttr) {this.h89rj = haerttr;}
    public String getKtysgb() {return hi893;}
    public char getHuiyhegh3() {return hjlr90t;}
    public boolean isHkjeo() {return hkjeo;}
    public String getHeser() {return hyz45kg;}
    public String getHaerttr() {return h89rj;}



    private final FragmentActivity activ_dababyity;

    private LinearLayout accoun_dababytGroup_dababyLayout;

    private ArrayList<Ba_dababynkAcc_dababyount_Fr> accoun_dababyts = new ArrayList<>();

    private final View.OnClickListener addAcco_dababyuntCl;

    private String bindChann_dababyelMobile = "";

    public BindAcco_dababyuntViewM_dababyanager_Fr(View view, Proces_dababysIn_dababyfo_Data data, String pageTag){
        super(view,data,pageTag);
        activ_dababyity = (FragmentActivity) view.getContext();
        addAcco_dababyuntCl = v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint(TAG,"ADD_CLICK","");
            new Bin_dababydAccount_dababyDialog_Fr(activ_dababyity, TAG,
                    mobile -> {
                        bindChann_dababyelMobile = mobile;
                        OkHttpUtil.getInstance_dababy().fetchWalletAccountList_fr(this::refreshAcc_dababyountList);
                    }
            ).show();
        };
     }

    @Override
    protected void in_dababyit(){
        accoun_dababytGroup_dababyLayout = mView.findViewById(R.id.ipbfa_layout_account_group);
        TextView tipsTitleTv_dababy = mView.findViewById(R.id.ipbfa_tv_tips_title);
        TextView tipsDescTv_dababy = mView.findViewById(R.id.ipbfa_tv_tips_desc);

        mView.findViewById(R.id.ipbfa_btn_add_account).setOnClickListener(v -> {
            addAcco_dababyuntCl.onClick(v);
        });

        haryhjkhr();
        String tipsTitle = App_dababyShow_dababyCustomize.getInstance_dababy().getBindAccountTipsTitle_dababy();
        String tipsDesc = App_dababyShow_dababyCustomize.getInstance_dababy().getBindAccountTipsDesc_dababy();

        if (!TextUtils.isEmpty(tipsTitle) && !TextUtils.isEmpty(tipsDesc)){
            tipsTitleTv_dababy.setText(tipsTitle);
            tipsDescTv_dababy.setText(tipsDesc);
        }

    }

    @Override
    public boolean checkI_dababynput() {
        return !accoun_dababyts.isEmpty();
    }

    @Override
    public String getInp_dababyut() {
        return String.valueOf(accoun_dababyts.size());
    }

    //更新列表
    public void refreshAcc_dababyountList(){

        stopLoading();

        accoun_dababytGroup_dababyLayout.removeAllViews();

        accoun_dababyts = Acc_dababyountLi_dababyst_Fr.getInstance_dababy().getBankAccountList_dababy();

        int num = 1;
        int i= accoun_dababyts.size()-1;
        while (i> accoun_dababyts.size()-3 || i>=0){

            if (i>=0) addAcc_dababyount(accoun_dababyts.get(i),num++);
            else {
                View view = View.inflate(mView.getContext(), R.layout.item_dababy_add_account_fr, null);
                TextView titleView = view.findViewById(R.id.iaafa_tv_title);
                titleView.setText(Base_dababyApplication.getInstant().getString(R.string.bind_card_mobile_title,num++));
                view.findViewById(R.id.iaafa_view_click).setOnClickListener(addAcco_dababyuntCl);
                accoun_dababytGroup_dababyLayout.addView(view);
            }

            i--;
        }

        //更新列表后，判断是否需要绑定运营商
        if (!TextUtils.isEmpty(bindChann_dababyelMobile) ){
            if (Othe_dababyrsInfo.getInstance_dababy().getAddBindFlag_dababy()){
                new Bin_dababydAcco_dababyuntChannelDialog_Fr(activ_dababyity, bindChann_dababyelMobile,
                        null,
                        () -> {
                            startLoading(activ_dababyity,"");
                            OkHttpUtil.getInstance_dababy().addWalletAccount_Fr(
                                    bindChann_dababyelMobile,
                                    bindChann_dababyelMobile,
                                    Othe_dababyrsInfo.getInstance_dababy().getChannel_dababy(),
                                    () -> OkHttpUtil.getInstance_dababy().fetchWalletAccountList_fr(this::refreshAcc_dababyountList),
                                    false
                            );
                        }

                ).show();
            }
        }

    }

    //添加卡
    private void addAcc_dababyount(Ba_dababynkAcc_dababyount_Fr account, int num){

        if (account == null) return;

        String desc = account.getWalletDesc_dababy();

        View accountView = View.inflate(mView.getContext(), R.layout.item_dababy_account_fr, null);
        TextView titleTv = accountView.findViewById(R.id.idaf_tv_title);
        ImageView logoIv = accountView.findViewById(R.id.idaf_iv_logo);
        TextView channelTv = accountView.findViewById(R.id.idaf_tv_channel);
        TextView mobileTv = accountView.findViewById(R.id.idaf_tv_mobile);
        ImageButton deleteIb = accountView.findViewById(R.id.idaf_ib_delete);

        titleTv.setText(Base_dababyApplication.getInstant().getString(R.string.bind_card_mobile_title,num));
        mobileTv.setText(account.getWalletMobile_dababy());
        channelTv.setText(account.getWalletDesc_dababy());

        //删除卡
        deleteIb.setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint(TAG,"DELETE_CLICK",account.getWalletMobile_dababy());
            new Delete_dababyAccountDi_dababyalog_Fr(activ_dababyity,account, TAG,
                    () -> OkHttpUtil.getInstance_dababy().fetchWalletAccountList_fr(this::refreshAcc_dababyountList)).show();
        });

        accoun_dababytGroup_dababyLayout.addView(accountView);

        //获取图标
        if (App_dababyShow_dababyCustomize.getInstance_dababy().getChannelLogoByDesc_dababy(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo_dababy())) return;
                    if (!App_dababyShow_dababyCustomize.getInstance_dababy().hasAccountChannel_dababy()) return;
                    URL url = new URL(account.getWalletLogo_dababy());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        App_dababyShow_dababyCustomize.getInstance_dababy().setChannelLogoByDesc_dababy(desc,bitmap);
                        DababyUtil.safeRun(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(App_dababyShow_dababyCustomize.getInstance_dababy().getChannelLogoByDesc_dababy(desc));

    }

    @Override
    public void inputEmptyTip() {

    }
}
